package com.china08.yxyapi.api.manage.edu.office.innernotice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner.YxyNotifictionTea;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 教育局行政办公 内部通知 搜索某个通知
 * 
 */
@CrossOrigin
@RestController("InnerNoticeReceiveOneSrhController")
public class InnerNoticeReceiveOneSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/office/innerNotice/receive/oneSrh", method = RequestMethod.POST)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        // 当前教育局id
        String currentSchoolId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String currentUserId = getProfile(request, response).getId();
        // 查询当前消息
        YxyNoticeInner yxyNoticeInner = yxyNoticeInnerRepository.findById(reqModel.id);

        String userNick = "";
        if (yxyNoticeInner.getUserId().equals(currentUserId)) {// 发送用户昵称
            userNick = yxyNoticeInner.getUserNick();
        } else {
            for (YxyNotifictionTea yxyNotifictionTea : yxyNoticeInner.getReceiver()) {// 接受用户昵称
                if (yxyNotifictionTea.getUserId().equals(currentUserId)) {
                    userNick = yxyNotifictionTea.getUserNick();
                    break;
                }
            }
        }
        RespModel respModel = new RespModel();
        respModel.currentSchoolId = currentSchoolId;
        // 不需要显示故删除
        respModel.currentSchoolNick = "";
        respModel.currentUserId = currentUserId;
        respModel.currentUserNickname = userNick;
        respModel.yxyNoticeInner = yxyNoticeInner;
        if (yxyNoticeInner != null) {
            Date date = yxyNoticeInner.getCreatedDate();
            if (date != null) {
                respModel.createDate = DateToolUtils.format(date, "yyyy-MM-dd");
            }
        }

        // ###
        // 已经查看人员数量
        List<YxyNotifictionTea> receiveList = yxyNoticeInner.getReceiver();
        Integer isReadCount = 0;
        Integer noReadCount = 0;
        List<Pojo> isReadList = new ArrayList<Pojo>();
        List<Pojo> noReadList = new ArrayList<Pojo>();
        for (YxyNotifictionTea tea : receiveList) {
            Integer isRead = tea.getIsRead();
            String userId = tea.getUserId();
            String uNick = tea.getUserNick();
            Pojo pojo = new Pojo();
            pojo.userId = userId;
            pojo.userNick = uNick;
            if (isRead == 1) {
                isReadCount = isReadCount + 1;
                isReadList.add(pojo);
            } else {
                noReadCount = noReadCount + 1;
                noReadList.add(pojo);
            }
        }
        respModel.isReadCount = isReadCount;
        respModel.noReadCount = noReadCount;
        respModel.isReadList = isReadList;
        respModel.noReadList = noReadList;

        // 点击详情时修改已读状态
        String isDetail = reqModel.isDetail;
        if (StringUtils.isNotBlank(isDetail) && isDetail.equals("true")) {
            // 修改为亿已经读取
            List<YxyNotifictionTea> list = yxyNoticeInner.getReceiver();
            for (YxyNotifictionTea yxyNotifictionTea : list) {
                // 邮件所属用户
                String userId = yxyNotifictionTea.getUserId();
                Integer isRead = yxyNotifictionTea.getIsRead();
                // 当前用户邮件，未读，未删除
                if (userId.equals(currentUserId) && isRead == 0) {
                    yxyNotifictionTea.setIsRead(1);
                    break;
                }
            }
            yxyNoticeInner.setReceiver(list);
            yxyNoticeInnerRepository.save(yxyNoticeInner);
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 内部通知id
        public String id;
        // 是否是详情请求
        public String isDetail;
    }

    public static class Pojo {
        public String userId;
        public String userNick;
    }

    protected static class RespModel {
        // 内部通知详细信息
        public YxyNoticeInner yxyNoticeInner;
        public String createDate;
        // 当前用户id
        public String currentUserId;
        // 当前用户昵称
        public String currentUserNickname;
        // 当前用户教育局id
        public String currentSchoolId;
        // 当前用户教育局昵称
        public String currentSchoolNick;
        public Integer isReadCount;
        public List<Pojo> isReadList;
        public Integer noReadCount;
        public List<Pojo> noReadList;
    }
}
